package com.dzp.simpleSyslogServer.common;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dzp.simpleSyslogServer.util.CommonUtil;

/**
 * @author dzp_java@qq.com
 */
public class CommonPool {
	private static Logger logger = LoggerFactory.getLogger(CommonPool.class);
	// 任务线程池
	private static int CommonExecutorsPoolSize = 50;
	public static ExecutorService CommonExecutorsPool = Executors.newFixedThreadPool(CommonExecutorsPoolSize);
	public static ExecutorService SingleThreadPool = Executors.newSingleThreadExecutor();

	/**
	 * 以下用于处理阻塞式 固定线程池
	 */
	private static BlockingQueue<Runnable> RunnableQueue = new ArrayBlockingQueue<Runnable>(CommonExecutorsPoolSize);
	public static ThreadPoolExecutor BlockingThreadPoolExecutor = new ThreadPoolExecutor(CommonExecutorsPoolSize / 2,
			CommonExecutorsPoolSize, 0L, TimeUnit.MILLISECONDS, RunnableQueue,
			new ThreadPoolExecutor.CallerRunsPolicy());

	/**
	 * 执行任务 无返回值
	 * 
	 * @throws Exception
	 */
	public static void executeTask(Thread thread) {
		try {
			// 提交任务
			CommonExecutorsPool.submit(thread);
		} catch (Exception e) {
			logger.error("-----任务提交失败---{}" + CommonUtil.getExceptionTrace(e));
		}
	}

	public static void executeTaskSingle(Thread thread) {
		try {
			// 提交任务
			SingleThreadPool.submit(thread);
		} catch (Exception e) {
			logger.error("-----任务提交失败---{}" + CommonUtil.getExceptionTrace(e));
		}
	}

	/**
	 * 执行任务 无返回值 使用锁定线程池
	 * 
	 * @throws Exception
	 */
	public static void executeTaskBlock(Thread thread) {
		try {
			BlockingThreadPoolExecutor.submit(thread);
		} catch (Exception e) {
			logger.error("-----任务提交失败---{}" + CommonUtil.getExceptionTrace(e));
		}
	}

}
